package mshop.com.schild_kotlin.activitys

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import kotlinx.android.synthetic.main.fragment_home.*
import mshop.com.schild_kotlin.R
import mshop.com.schild_kotlin.adapters.HomeAdapter
import mshop.com.schild_kotlin.base.BaseActivity
import mshop.com.schild_kotlin.entitys.HotBean
import mshop.com.schild_kotlin.entitys.ItemList
import mshop.com.schild_kotlin.mvp.base.BaseContract
import mshop.com.schild_kotlin.mvp.presenter.CategoryPresener

/**
 * Created by mayi on 2018/5/30.
 *
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO  发现 -- 分类
 */
class CateGoryActivity : BaseActivity(), BaseContract.View<HotBean>, View.OnClickListener {
    var mPresener: CategoryPresener? = null
    var categoryName: String = ""
    var homeAdapter: HomeAdapter? = null
    var mList = ArrayList<ItemList>()


    //点击事件
    override fun onClick(p0: View?) {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        categoryName = intent.extras.getString("categoryName", "")
        initView()
    }

    override fun getLayoutResources(): Int {
        return R.layout.activity_category
    }

    override public fun initView() {
        commTitleBar(categoryName, View.VISIBLE, View.INVISIBLE, 0)

        homeAdapter = HomeAdapter(mList)
        recycle_view.layoutManager = LinearLayoutManager(applicationContext)
        recycle_view.adapter = homeAdapter
        homeAdapter?.notifyDataSetChanged()

        mPresener = CategoryPresener(applicationContext, this)

        recycle_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                val layoutManager: LinearLayoutManager = recyclerView?.layoutManager as LinearLayoutManager
                val lastPosition = layoutManager.findLastVisibleItemPosition()
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastPosition >= mList.size - 3) {
                    if (!TextUtils.isEmpty(categoryName)) {
                        mPresener?.getMoreCategory(mList.size, 20, categoryName)
                    }
                }
            }
        })
    }


    override fun setData(entity: HotBean) {
        if (refreshLayout!!.isRefreshing) {
            refreshLayout?.isRefreshing = false
            if (mList.size > 0) {
                mList.clear()
            }
        }

        entity.itemList
                .filter { it.type.equals("video") }
                .forEach { mList.add(it) }

        homeAdapter?.notifyDataSetChanged()
    }

    override fun onRefresh() {
        mPresener?.getFirstCategory(categoryName)
    }

    override fun getNeedRefresh(): Boolean {
        return true
    }

}
